#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QMutex>
#include <QTextStream>
#include <QDebug>
#include <QDateTime>
#include <QFile>
#include "ConfigEditor/configanalyze.h"
#include "maincontroller.h"
#include "SqlTable/SqlRecordCountTableModel.h"


static MainController main_controller;

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{

    static QMutex mutex;
    mutex.lock();
    emit main_controller.signalDebuging(msg);
    mutex.unlock();

}


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);


    //qInstallMessageHandler(outputMessage);
    QGuiApplication app(argc, argv);

    SqlRecordCountTableModel json_table;
    ComboBoxModel section_combo;
    ComboBoxModel key_combo;

    ConfigAnalyze analyze;
    analyze.setTable(&json_table);
    analyze.setCombobox(&section_combo,&key_combo);

    json_table.appendColumn("title01");
    json_table.appendColumn("title02");
//    json_table.appendColumn("title03");
//    json_table.appendColumn("title04");

    json_table.appendRow("row1",(QVariantList()<<"1"<<"1"));
    json_table.appendRow("row2",(QVariantList()<<"1"<<"1"));

    QQmlApplicationEngine engine;

    QQmlContext *classContext = engine.rootContext();
    classContext->setContextProperty("sectionComboModel", &section_combo);

    QQmlContext *classContext1 = engine.rootContext();
    classContext1->setContextProperty("keyComboModel", &key_combo);

    engine.rootContext()->setContextProperty("MainController",&main_controller);
    engine.rootContext()->setContextProperty("JsonAnalyze",&analyze);
    engine.rootContext()->setContextProperty("JsonTable",&json_table);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}
